package com.dong.controller.admin;


import com.dong.config.TitleConfig;
import com.dong.service.GoodService;
import com.dong.vo.GoodResult;
import com.dong.vo.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@RequestMapping("admin/order")
@Controller
public class OrderController {

    @Autowired
    private GoodService goodService;
    @Autowired
    private TitleConfig titleConfig;

    @GetMapping("list")
    public String list(Model model, @RequestParam(value = "name",required = false,defaultValue = "") String name, PageResult pageResult){
        model.addAttribute("title", titleConfig.getGoodTitle());
        model.addAttribute("name",name );
        PageResult<GoodResult> goodResultPageResult = goodService.selectPages(pageResult, name);

        List<GoodResult> content = goodResultPageResult.getContent();
        if (content.isEmpty()){
            model.addAttribute("pageResult",goodService.selectPages(pageResult,name ));
            return "/admin/order/list";
        }

        List<GoodResult> collect = content.stream().filter(s -> 5 == s.getStatus()).collect(Collectors.toList());
        PageResult<GoodResult> result = new PageResult<>();
        result.setContent(collect);
        result.setPageSize(goodResultPageResult.getPageSize());
        result.setCurrentPage(goodResultPageResult.getCurrentPage());
        result.setTotal(goodResultPageResult.getTotal());
        result.setTotalPage(goodResultPageResult.getTotalPage());

        model.addAttribute("pageResult",result);
        return "/admin/order/list";
    }
}
